package _interview100;

import java.util.HashSet;

/**
 * 3. 无重复字符的最长子串
 */
public class No3 {
    public int lengthOfLongestSubstring(String s) {
        int left = 0, right = 0, answer = 0;
        HashSet<Character> hashSet = new HashSet<>();
        for (; right < s.length(); right++) {
            if (hashSet.contains(s.charAt(right))) {
                while (s.charAt(left) != s.charAt(right)) {
                    hashSet.remove(s.charAt(left));
                    left++;
                }
                hashSet.remove(s.charAt(left));
                left++;
            }
            answer = Math.max(answer, right - left + 1);
            hashSet.add(s.charAt(right));
        }
        return answer;
    }
}
